package com.situ.mall.web.controller;

import com.situ.mall.common.entity.Cart;
import com.situ.mall.common.entity.Member;
import com.situ.mall.common.exception.NoLoginException;
import com.situ.mall.common.service.ICartService;
import com.situ.mall.common.util.RequestUtil;
import com.situ.mall.common.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.List;

//购物车
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private ICartService cartService;
    @GetMapping("/list")
    public String page(Model model) {
       Member m =(Member)RequestUtil.getCurrent();
        List<CartVO> list= cartService.Cart(m.getId());
        model.addAttribute("carts",list);
        return "cart";
    }

    @PostMapping("/save")
    @ResponseBody
    public void save(Cart cart){
        Object o = RequestUtil.getCurrent();
        Member m = (Member) o;
        if (o == null){//没登录
            throw new NoLoginException();
        } else {//登录过跳进购物车
            cart.setJoinTime(LocalDateTime.now());
            cart.setMemberId( m.getId());
            cartService.save(cart);
        }
    }
    @PostMapping("/delete")
    @ResponseBody
    public void delete(Integer id){
        cartService.delete(id);
    }

    @PostMapping("/number")
    @ResponseBody
    public void update(Integer id,Integer number){
        cartService.update(id,number);
    }


}
